﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace BPM.Common
{
	[DebuggerDisplay("{Success}:{Message}")]
	public struct Result
	{
		private bool _success;

		private string _message;

		public Result(bool success)
		{
			this._success = success;
			this._message = success ? "操作成功。" : "操作失败。";
		}

		public Result(string message)
		{
			this._success = false;
			this._message = message;
		}

		public Result(bool success, string message)
		{
			this._success = success;
			this._message = message;
		}

		public string Message
		{
			get
			{
				return this._message;
			}
			set
			{
				if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
				this._message = value;
			}
		}

		public bool Success
		{
			get
			{
				return this._success;
			}
			set
			{
				this._success = value;
			}
		}

		public Result ReplaceMessage(string success, string failed)
		{
			this.Message = this.Success ? success : failed;
			return this;
		}

		public static implicit operator bool(Result value)
		{
			return value.Success;
		}

		public static implicit operator string(Result value)
		{
			return value.Message;
		}

		public static implicit operator Result(bool value)
		{
			return new Result(value);
		}

		public static implicit operator Result(string message)
		{
			return new Result(message);
		}
	}
}
